package com.tb.ClassSchedule;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by guich on 2016/8/17.
 */
public class AddClassActivity extends AppCompatActivity implements View.OnClickListener{

    private Toolbar toolbar;

    public static Button             selectWeek;
    public static Button           selectSection;
    private TextView tvClassName;
    private TextView tvTeacherName;
    private TextView tvAddress;

    public static AddClassActivity main;

    private String className;
    private String teacherName;
    public static int startWeek;
    public static int endWeek;
    public static int weekday;
    public static int startSection;
    public static int endSection;
    private String address;

    private LinearLayout delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_class);

        initToolBar();
        main = this;


        selectWeek = (Button) findViewById(R.id.select_week);
        selectWeek.setOnClickListener(this);
        selectSection = (Button) findViewById(R.id.select_section);
        selectSection.setOnClickListener(this);
        tvClassName = (TextView) findViewById(R.id.class_name);
        tvTeacherName = (TextView) findViewById(R.id.teacher_name);
        tvAddress = (TextView) findViewById(R.id.class_address);
        delete = (LinearLayout) findViewById(R.id.detail_delete);
        delete.setVisibility(View.GONE);
    }

    private void initToolBar(){
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("添加课程");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            toolbar.setPadding(0,MainActivity.statusBarHeight,0,0);
        }
        toolbar.setBackgroundColor(MainActivity.sharedPreferences.getInt("color",MainActivity.DEFAULT_THEME));
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if(item.getItemId() == R.id.action_commit){
                    className = tvClassName.getText().toString();
                    teacherName = tvTeacherName.getText().toString();
                    address = tvAddress.getText().toString();

                    if(className.equals("") ||
                            selectSection.getText().equals("") ||
                            selectWeek.getText().equals("")){
                        Log.d("检测添加条件","不合法");
                        AlertDialog.Builder builder = new AlertDialog.Builder(main);
                        builder.setTitle("警告！");
                        builder.setMessage("\n课程名称和上课时间都不给，臣妾做不到啊~");
                        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        });
                        builder.show();

                    }else {
                        saveToDatabase();
                        Log.d("添加成功",className + "\n"
                                + teacherName + "\n"
                                + address + "\n"
                                + startWeek + " ~ "+ endWeek + "\n"
                                + weekday + " : " + startSection + " ~ " + endSection + "\n"
                        );

                        Toast.makeText(main,"添加成功",Toast.LENGTH_SHORT).show();
                        MainActivity.main.draw();
                        main.finish();

                    }
                }
                return true;
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_commit,menu);
        return true;
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.select_section:
                SectionSelector sectionSelector = new SectionSelector(this);
                sectionSelector.show();
                break;
            case R.id.select_week:
                WeekSelector weekSelector = new WeekSelector(this);
                weekSelector.show();
                break;
        }
    }

    private void saveToDatabase(){

        LessonDatabaseHelper dbHelper = new LessonDatabaseHelper(MainActivity.main, "Lessons.db", null, 1);
        SQLiteDatabase       db       = dbHelper.getWritableDatabase();
        ContentValues                        values   = new ContentValues();
        values.put("ClassName", className);
        values.put("WeekDay", weekday);
        values.put("StartSection", startSection);
        values.put("EndSection", endSection);
        values.put("StartWeek", startWeek);
        values.put("EndWeek", endWeek);
        values.put("TeacherName", teacherName);
        values.put("Address", address);
        db.insert("Lesson", null, values); //  插入第一条数据
        values.clear();

    }



}
